package org.zhadoop.stream.cql.semanticanalyzer.parser.context;

import java.util.List;

import org.zhadoop.stream.cql.DriverContext;
import org.zhadoop.stream.cql.exception.CQLException;
import org.zhadoop.stream.cql.exception.SemanticAnalyzerException;
import org.zhadoop.stream.cql.hooks.SemanticAnalyzeHook;
import org.zhadoop.stream.cql.semanticanalyzer.SelectClauseAnalyzer;
import org.zhadoop.stream.cql.semanticanalyzer.SemanticAnalyzer;
import org.zhadoop.stream.cql.semanticanalyzer.parsecontextwalker.ParseContextWalker;
import org.zhadoop.stream.cql.tasks.Task;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;

/**
 * select 语句解析内容
 * @author zwd
 *
 */
public class SelectClauseContext extends ParseContext{
	
	private boolean isDistinct = false;
	
	 private List<SelectItemContext> selectItems;

	 public SelectClauseContext() {
		 selectItems = Lists.newArrayList();
	}
	 
	 
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
        sb.append("SELECT ");
        sb.append(isDistinct ? "DISTINCT " : "");
        sb.append(Joiner.on(", ").join(selectItems));
        return sb.toString();
	}
	
	

	public boolean isDistinct() {
		return isDistinct;
	}


	public void setDistinct(boolean isDistinct) {
		this.isDistinct = isDistinct;
	}


	public List<SelectItemContext> getSelectItems() {
		return selectItems;
	}


	public void setSelectItems(List<SelectItemContext> selectItems) {
		this.selectItems = selectItems;
	}


	@Override
	public Task createTask(DriverContext driverContext, List<SemanticAnalyzeHook> analyzeHooks) throws CQLException {
		return null;
	}

	@Override
	public SemanticAnalyzer createAnalyzer() throws SemanticAnalyzerException {
		return new SelectClauseAnalyzer(this);
	}


	@Override
	protected void walkChild(ParseContextWalker walker) {
		for(SelectItemContext item : selectItems){
			walkExpression(walker, item);
		}
	}

}
